home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Graphics / source / bsptree_3d.cp < prev    next >
Encoding:
Text File  |  1995-04-05  |  5.7 KB  |  100 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    bsptree.cp
  3. //    Date:                    9/25/94
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains the methods for a bsp tree
  7. //
  8. //------------------------------------------------------------------------------
  9.  
  10. #include "bspnode_3d.h"
  11.  
  12. //------------------------------------------------------------------------------
  13. //    constructor
  14. //------------------------------------------------------------------------------
  15. bsptree::bsptree (void)                                                                                                                    //    constructor
  16. {                                                                                                                                                                //    begin
  17.     node = 0;                                                                                                                                            //    no dangling pointers
  18. }                                                                                                                                                                //    end
  19.  
  20. //------------------------------------------------------------------------------
  21. //    destructor
  22. //------------------------------------------------------------------------------
  23. bsptree::~bsptree (void)                                                                                                                //    destructor
  24. {                                                                                                                                                                //    begin
  25.     if (node)                                                                                                                                            //    if this is a leaf in the tree
  26.         delete node;                                                                                                                                //    delete the structure data
  27. }                                                                                                                                                                //    end
  28.  
  29. //------------------------------------------------------------------------------
  30. //    insert a list of faces into the tree
  31. //------------------------------------------------------------------------------
  32. void    bsptree::Insert (listptr list, hclass keep, hclass cur)                                        //    insert a list of polygons into the tree
  33. {                                                                                                                                                                //    begin
  34.     if (list->Empty ()) return;                                                                                                        //    don't do anything if the list is empty
  35.     if (node)                                                                                                                                            //    if the tree is valid
  36.         node->Insert (list, keep);                                                                                                    //    insert the polyptr list
  37.     else                                                                                                                                                    //    otherwise
  38.         if ((cur == keep) || (keep == SPANNING))                                                                        //    if the current node is the kind we want
  39.         {                                                                                                                                                        //    begin
  40.             node = new bspnode (list->Pop ());                                                                                //    create the leaf representation with first polyptr in the list
  41.             if (!list->Empty ())                                                                                                            //    if the list is not empty now
  42.                 node->Insert (list, SPANNING);                                                                                    //    insert the remaining polyptr list
  43.         }                                                                                                                                                        //    end
  44. }                                                                                                                                                                //    end
  45.  
  46. //------------------------------------------------------------------------------
  47. //    push a face through the tree
  48. //------------------------------------------------------------------------------
  49. void    bsptree::Push (polyptr poly, listptr result, hclass keep, hclass cur)            //    push a polyptr through the tree
  50. {                                                                                                                                                                //    begin
  51.     if (node)                                                                                                                                            //    if the tree is valid
  52.         node->Push (poly, result, keep);                                                                                        //    push the polyptr
  53.     else                                                                                                                                                    //    otherwise
  54.         if (cur == keep)                                                                                                                        //    if the current node is the kind we want
  55.             result->AddToList (poly);                                                                                                    //    add the polyptr to the list
  56. }                                                                                                                                                                //    end
  57.  
  58. //------------------------------------------------------------------------------
  59. //    push a list of faces through the tree
  60. //------------------------------------------------------------------------------
  61. void    bsptree::Push (listptr list, listptr result, hclass keep, hclass cur)            //    push a list of polygons through the tree
  62. {                                                                                                                                                                //    begin
  63.     if (list->Empty ()) return;                                                                                                        //    don't do anything if the list is empty
  64.     if (node)                                                                                                                                            //    if the tree is valid
  65.         node->Push (list, result, keep);                                                                                        //    push the polyptr list
  66.     else                                                                                                                                                    //    otherwise
  67.         if (cur == keep)                                                                                                                        //    if the current node is the kind we want
  68.             result->Append (list);                                                                                                        //    append the list to the results
  69. }                                                                                                                                                                //    end
  70.  
  71. //------------------------------------------------------------------------------
  72. //    reduce to boundary
  73. //------------------------------------------------------------------------------
  74. void    bsptree::Reduce (void)                                                                                                        //    reduce the tree to only boundary polygons
  75. {                                                                                                                                                                //    begin
  76.     if (node)                                                                                                                                            //    if the tree is valid
  77.         node->Reduce ();                                                                                                                        //    compute the boundary representation
  78. }                                                                                                                                                                //    end
  79.  
  80. //------------------------------------------------------------------------------
  81. //    draw
  82. //------------------------------------------------------------------------------
  83. void    bsptree::Draw (const point_3d &eye) const                                                                    //    draw the bsp
  84. {                                                                                                                                                                //    begin
  85.     if (node)                                                                                                                                            //    if the tree is valid
  86.         node->Draw (eye);                                                                                                                        //    draw it
  87. }                                                                                                                                                                //    end
  88.  
  89. //------------------------------------------------------------------------------
  90. //    intersect with ray
  91. //------------------------------------------------------------------------------
  92. bool    bsptree::RayIntersection (const ray &r, polyptr &poly_hit, point_3d &ipt) const    //    compute the polygon intersected by a ray
  93. {                                                                                                                                                                //    begin
  94.     if (node)                                                                                                                                            //    if the tree is valid
  95.         return node->RayIntersection (r, poly_hit, ipt);                                                        //    trace it
  96.     return FALSE;                                                                                                                                    //    return a no hit
  97. }                                                                                                                                                                //    end
  98.  
  99. //------------------------------------------------------------------------------
  100.